package com.community.web.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.model.entity.PortalWalletLog;
import com.community.model.entity.Wallet;
import com.community.web.user.mapper.PortalWalletLogMapper;
import com.community.web.user.mapper.WalletMapper;
import com.community.web.user.service.PortalWalletLogService;
import com.community.web.user.vo.WalletLogQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
* @author admin
* @description 针对表【portal_wallet_log】的数据库操作Service实现
* @createDate 2024-07-03 15:25:23
*/
@Service
public class PortalWalletLogServiceImpl extends ServiceImpl<PortalWalletLogMapper, PortalWalletLog>
    implements PortalWalletLogService {

    @Autowired
    private WalletMapper walletMapper;

    @Override
    public void addLog(PortalWalletLog walletLog) {
        // 1. 生成当前日期时间的格式化字符串
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String formattedDate = dateFormat.format(new Date());

        // 2. 生成一个随机的数字串（这里生成了10位的随机数）
        Random random = new Random();
        String randomDigits = String.format("%010d", random.nextInt(1000000000));

        // 3. 将日期时间字符串和随机数字串拼接起来
        String generatedString = formattedDate + randomDigits;

        if (walletLog.getType() == 1){
            walletLog.setOrderNo(generatedString);
        }else if (walletLog.getType() == 2){
            walletLog.setOrderNo("WALLET_RECHARGE");
        }else if (walletLog.getType() == 3){
            walletLog.setOrderNo("WALLET_TRANSFER_IN");
        }else if (walletLog.getType() == 4){
            walletLog.setOrderNo("WALLET_TRANSFER_OUT");
        }else if (walletLog.getType() == 5){
            walletLog.setOrderNo("WALLET_REFUND");
        }else if (walletLog.getType() == 6){
            walletLog.setOrderNo("C" + generatedString);
        }
        super.save(walletLog);
    }

    @Override
    public IPage<PortalWalletLog> pageList(Long userId, Page<PortalWalletLog> page, WalletLogQueryVo queryVo) {
        Wallet wallet = walletMapper.getByUserId(userId);
        LambdaQueryWrapper<PortalWalletLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalWalletLog::getWalletId, wallet.getId());
        queryWrapper.eq(queryVo.getState() != 0, PortalWalletLog::getState, queryVo.getState());
        queryWrapper.eq(queryVo.getType() != 0, PortalWalletLog::getType, queryVo.getType());
        return super.page(page, queryWrapper);

    }
}




